import jdk.nashorn.internal.ir.CallNode;
import org.junit.Test;

import java.io.*;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws Exception {

//        testDecompress(new File("file//88abcc2e3c998bebbab13c0e3bdf3a8f612349"));

        String filename = "zhangsan";
        String property = System.getProperty("user.dir");
        new File("ff/ffdsa//fd").mkdirs();
        System.out.println("执行命令的目录是："+property);
    }



    public static void testDecompress(File file) throws Exception{
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer =new byte[1024]; int len;
        while ((len=fis.read(buffer)) != -1){
            //这个String方法  中间是从数组下标为0的开始读取，b是读取4个，c是得出用数组读取了几个元素
            baos.write(buffer,0,len);
        }
        byte[] bytes = baos.toByteArray();
        byte[] decompress = ZLibUtils.decompress(bytes);

        FileOutputStream fos = new FileOutputStream("file"+File.separator+"decompressed-"+file.getName());
        fos.write(decompress);
        fos.close();
        fis.close();


    }




    @Test
    public void testWriteBlob() throws Exception {
        byte[] content = "Java可能是东半球最好的编程语言".getBytes();
        String hash = HashUtils.getHashOfByteArray(content);
//        writeBlob1(content, hash);
        writeBlob3(content,hash);
        readBlob3("file"+File.separator+hash);
    }


    public static void writeBlob1(byte[] content, String hash) throws Exception{
        FileOutputStream fos = new FileOutputStream("file"+File.separator+hash);
        fos.write("blob ".getBytes());
        fos.write(String.valueOf(content.length).getBytes());
        fos.write(0);
        fos.write(content);
        fos.close();
    }


    public static void writeBlob3(byte[] content, String hash) throws Exception{
        Blob blob = new Blob(content, content.length, hash);
        FileOutputStream fos = new FileOutputStream("file"+File.separator+hash);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(blob);
        oos.close();
    }


    public static void readBlob3(String filepath) throws Exception{
        FileInputStream fis = new FileInputStream(filepath);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Blob blob =(Blob) ois.readObject();
        ois.close();
        System.out.println(blob);


    }

}

class Blob implements Serializable {
    private static final long serialVersionUID = 134252411453L;

    private byte[] content;
    private long length;
    private String hash;

    private String name;

    public Blob(byte[] content, long length, String hash) {
        this.content = content;
        this.length = length;
        this.hash = hash;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

    public long getLength() {
        return length;
    }

    public void setLength(long length) {
        this.length = length;
    }

    public String getHash() {
        return hash;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }

    @Override
    public String toString() {
        return "Blob{" +
                "content=" + new String(content) +
                ", length=" + length +
                ", hash='" + hash + '\'' +
                '}';
    }
}